<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>用户设置</title>
<script type="text/javascript">
    var newEmployeeWin;
    var editEmployeeWin;
    var employeeId;
    var employeeDS;
    //处理员工锁定
    function lockEmployeeFn(id) {
        Ext.Ajax.request({
            url: '/employee/employeeLock',
            success: function (result, request) {
                employeeDS.load();
            },
            failure: function (result, request) {
                Ext.MessageBox.show({
                    title: '消息',
                    msg: "通讯失败，请从新操作",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                });
            },
            method: 'GET',
            params: {
                id: id
            }
        });
    }

    Ext.onReady(function () {
        //用户设置---------------------------------------------------------
        //生成有关用户的Grid
        employeeDS = Ext.create('Ext.data.Store', {
            //autoDestroy : true,
            model: 'Employee',
            proxy: {
                type: 'ajax',
                url: '/employee/getAllEmployees',
                reader: {
                    type: 'json',
                    root: 'root',
                    totalProperty: 'totalProperty'// 数据集记录总数
                }
            },
            pageSize: pageSize + 15,
            autoLoad: true
        });

        var employeeCM = Ext.create('Ext.grid.column.Column', {defaults: {
            sortable: true,
            menuDisabled: false
        }, columns: []});

        //锁定用户
        function renderEmployeeIsLucked(value, cellmeta, record, index, columnIndex, store) {
            if (record.get("is_locked") == "1") {
                return "<a style=cursor:pointer onclick=lockEmployeeFn(" + store.getAt(index).get('id') + ")><em style=color:red>锁定</em></a>";
            } else {
                return "<a style=cursor:pointer onclick=lockEmployeeFn(" + store.getAt(index).get('id') + ")><em style=color:green>未锁定</em></a>";
            }
        }

        var employeeGrid = Ext.create('Ext.grid.Panel', {
            store: employeeDS,
            columns: [Ext.create('Ext.grid.RowNumberer'), {
                header: 'id',
                dataIndex: 'id',
                width: 30
            }, {
                header: '公司',
                dataIndex: 'company_name',
                width: 50
            }, {
                header: '是否锁定',
                dataIndex: 'is_locked',
                renderer: renderEmployeeIsLucked,
                width: 60
            }, {
                header: '编号',
                dataIndex: 'number',
                width: 45
            }, {
                header: '登录Id',
                dataIndex: 'login_id',
                width: 70
            }, {
                header: '名称',
                dataIndex: 'name',
                width: 60
            }, {
                header: '是否离职',
                dataIndex: 'quit_job',
                width: 70
            }, {
                header: '部门',
                dataIndex: 'department_name',
                width: 70
            }, {
                header: '角色',
                dataIndex: 'role_name',
                width: 90
            }, {
                header: '身份证',
                dataIndex: 'identity_card'
            }, {
                header: '电话',
                dataIndex: 'phone'
            }, {
                header: '地址',
                dataIndex: 'address',
                width: 200
            }, {
                header: '电子邮箱',
                dataIndex: 'email',
                width: 180
            }, {
                header: '描述',
                dataIndex: 'description',
                hidden: true,
                width: 230
            }, {
                header: '修改日期',
                dataIndex: 'updated_at',
                renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
                width: 115
            }],
            selModel: Ext.create('Ext.selection.CheckboxModel'),
            width: screenWidth - 190,
            height: screenHeight - 285,
            iconCls: 'icon-grid',
            //collapsible: true,
            //animCollapse: false,
            //frame: true,
            //title: '用户列表',
            bbar: Ext.create('Ext.toolbar.Paging', {
                pageSize: pageSize + 15,
                store: employeeDS,
                displayInfo: true,
                displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
                emptyMsg: "没有记录"
            }),
            tbar: [
                {
                    text: '添加',
                    iconCls: 'addItem',
                    handler: function () {
                        newEmployeeWin = Ext.create('Ext.window.Window', {
                            layout: 'fit',
                            width: 617,
                            height: 330,
                            closeAction: 'destroy',
                            plain: true,
                            modal: true,
                            constrain: true,
                            //modal: true,
                            title: '添加用户',
                            autoLoad: {
                                url: "/employee/newEmployee",
                                scripts: true
                            }
                        });
                        newEmployeeWin.on("destroy", function () {
                            employeeDS.load();
                        });
                        newEmployeeWin.resizable = false;
                        newEmployeeWin.show();
                    }
                },
                '-',
                {
                    text: '编辑',
                    iconCls: 'editItem',
                    handler: function () {
                        var records = employeeGrid.getSelectionModel().getSelection();
                        if (records.length == 0) {
                            Ext.MessageBox.show({
                                title: '提示信息',
                                msg: "请先选中一条记录后，再编辑。",
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.WARNING
                            });
                        } else {
                            //把表单添加到窗口中
                            employeeId = records[0].get('id');
                            editEmployeeWin = Ext.create('Ext.window.Window', {
                                title: '编辑用户',
                                layout: 'fit',
                                width: 617,
                                height: 335,
                                closeAction: 'destroy',
                                constrain: true,
                                plain: true,
                                modal: true,
                                autoLoad: {
                                    url: "/employee/editEmployee?id=" + employeeId,
                                    scripts: true
                                }
                            });
                        }
                        editEmployeeWin.on("destroy", function () {
                            employeeDS.load();
                        });
                        editEmployeeWin.resizable = false;
                        editEmployeeWin.show();
                    }
                }
            ]
        });
        employeeGrid.render('employee_list');
    })
</script>
</head>
<body>
<div id="employee_list"></div>
</body>
</html>
